package com.ebelter.nb.ui.activitys;


import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;
import com.ebelter.btcomlib.models.bluetooth.products.nb.BondDevices;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.nb.R;
import com.ebelter.nb.utils.NbUtitls;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


public class AddDeviceResultActivity extends NBBaseActivity implements View.OnClickListener {


    public static final String TAG = "AddDeviceResultActivity";
    public static String ProductStyles = "ProductStyles";


    public static final int AddDeviceResultActivity_Request = 2;
    public static final int BDQT = -2;

    private ImageView top_left_iv;

    private TextView add_d_r_name_tv;
    private ImageView add_d_r_product_iv;

    private Button add_d_r_bdqt_bt;//绑定其他按钮
    private Button add_d_r_wch_bt;//完成按钮


    private Handler mHandler=new Handler();


    private ProductStyle productStyle;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_adddeviceresult;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
        setListener();
    }

    @Override
    protected void initData() {


        productStyle = (ProductStyle) getIntent().getSerializableExtra(ProductStyles);


        if (productStyle == null) {
            throw new NullPointerException("productStyle");
        }

        if (productStyle== ProductStyle.BPM){
            ViewUtils.setTextViewStr(add_d_r_name_tv,"血压计");
            ViewUtils.setImageResource(add_d_r_product_iv,R.drawable.ic_bpm);
        }else if (productStyle== ProductStyle.SCALE){
            ViewUtils.setTextViewStr(add_d_r_name_tv,"体脂秤");
            ViewUtils.setImageResource(add_d_r_product_iv,R.drawable.ic_scale);
        }

        disNewestViews();
    }

    private void setListener() {

        top_left_iv.setOnClickListener(this);
        add_d_r_bdqt_bt.setOnClickListener(this);
        add_d_r_wch_bt.setOnClickListener(this);

    }

    private void FV() {
        top_left_iv = findViewById(R.id.top_left_iv);
        TextView title_tv = findViewById(R.id.m_title_tv);
        TextView rightTitle_tv = findViewById(R.id.m_righttitle_tv);
        ViewUtils.setTextViewStr(title_tv, "绑定设备");
        ViewUtils.hideView(rightTitle_tv);

        add_d_r_name_tv = findViewById(R.id.add_d_r_name_tv);
        TextView add_d_r_connectstation_tv = findViewById(R.id.add_d_r_connectstation_tv);
        add_d_r_product_iv = findViewById(R.id.add_d_r_product_iv);

        add_d_r_bdqt_bt = findViewById(R.id.add_d_r_bdqt_bt);
        add_d_r_wch_bt = findViewById(R.id.add_d_r_wch_bt);

    }


    private void disNewestViews(){
            ViewUtils.displayView(add_d_r_bdqt_bt);
            ViewUtils.displayView(add_d_r_wch_bt);
            List<BondDevices> list = NbUtitls.queryBondedDevices();
            if (list==null||list.size()==0||list.size()==1){
                //说明没有绑定 或者绑定了一个
            }else{
                //都绑定完成了
                ViewUtils.goneView(add_d_r_bdqt_bt);
            }

    }


    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.top_left_iv) {
            finish();
        }else if (vid == R.id.add_d_r_bdqt_bt){//绑定其他
            setResult(BDQT);
            finish();
        }else if (vid == R.id.add_d_r_wch_bt){//完成

            if (productStyle==ProductStyle.BPM){
                ActivityManagers.getInstance().closeActivity(MainActivity.TAG);
                ActivityManagers.getInstance().closeActivity(AddDeviceActivity.TAG);

                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Bundle bundle=new Bundle();
                        bundle.putBoolean(MainActivity.GetBandListFromNet,false);
                        bundle.putBoolean(MainActivity.GetUserInfoFromNet,false);
                        startActivity(MainActivity.class,bundle);
                        finish();
                    }
                },600);


            }else if (productStyle == ProductStyle.SCALE){


                if (!NbUtitls.infoIsWanQuan()){//判断资料是否完全
                    //如果不齐全 跳转到体脂秤补全信息页面
                    startActivity(AddInfoActivity.class);
                }else {
                    //如果齐全 关闭信息不全的对话框
                    CommonEventBus commonEventBus=new CommonEventBus(TAG,MainActivity.TAG,10,null);
                    EventBus.getDefault().post(commonEventBus);

                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ActivityManagers.getInstance().closeActivity(MainActivity.TAG);
                            //如果资料齐全 直接跳转到主界面
                            Bundle bundle = new Bundle();
                            bundle.putBoolean(MainActivity.GetBandListFromNet,false);
                            bundle.putBoolean(MainActivity.GetUserInfoFromNet,false);
                            bundle.putInt(MainActivity.WhichUser,productStyle== ProductStyle.BPM?0:1);
                            startActivity(MainActivity.class,bundle);
                            ActivityManagers.getInstance().closeActivity(AddDeviceActivity.TAG);
                            finish();
                        }
                    },600);


                }

            }


        }
    }


}
